package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.domain.vo.JobApplyWithJobVO;
import com.ruoyi.business.service.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.JobApplyMapper;

/**
 * 岗位投递Service业务层处理
 * 
 * @author asher
 * @date 2021-11-22
 */
@Service
public class JobApplyServiceImpl implements IJobApplyService 
{
    @Autowired
    private JobApplyMapper jobApplyMapper;

    @Autowired
    private IMessageCenterService messageCenterService;

    @Autowired
    private IRecruitJobsService recruitJobsService;

    @Autowired
    private IAuthBindingInfoService authBindingInfoService;

    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询岗位投递
     * 
     * @param applyId 岗位投递主键
     * @return 岗位投递
     */
    @Override
    public JobApply selectJobApplyByApplyId(Long applyId)
    {
        return jobApplyMapper.selectJobApplyByApplyId(applyId);
    }

    /**
     * 查询岗位投递列表
     * 
     * @param jobApply 岗位投递
     * @return 岗位投递
     */
    @Override
    public List<JobApply> selectJobApplyList(JobApply jobApply)
    {
        return jobApplyMapper.selectJobApplyList(jobApply);
    }

    /**
     * 新增岗位投递
     * 
     * @param jobApply 岗位投递
     * @return 结果
     */
    @Override
    public int insertJobApply(JobApply jobApply)
    {
        RecruitJobs recruitJobs = recruitJobsService.selectRecruitJobsByJobId(jobApply.getJobId());
        if (null == recruitJobs) {
            return 0;
        }
        List<AuthBindingInfo> authBindingInfos = authBindingInfoService.selectAuthBindingInfoByWxUserIdAndStatus(jobApply.getUserId(), "2");
        if (authBindingInfos.size() == 0) {
            return 0;
        }
        WxUser wxUser = wxUserService.selectWxUserByUserId(jobApply.getUserId());
        if (null == wxUser) {
            return 0;
        }

        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("岗位投递提醒");
        String content = "您的岗位有人进行了投递，请尽快联系！岗位名称：" + recruitJobs.getName() + ", 投递人：" + authBindingInfos.get(0).getRealName() +
                "联系方式：" + wxUser.getPhone();
        messageCenter.setContent("[岗位投递]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("岗位投递");
        messageCenter.setDestUserId(recruitJobs.getMasterId().toString());
        messageCenterService.insertMessageCenter(messageCenter);

        jobApply.setCreateTime(DateUtils.getNowDate());
        return jobApplyMapper.insertJobApply(jobApply);
    }

    /**
     * 修改岗位投递
     * 
     * @param jobApply 岗位投递
     * @return 结果
     */
    @Override
    public int updateJobApply(JobApply jobApply)
    {
        jobApply.setUpdateTime(DateUtils.getNowDate());
        return jobApplyMapper.updateJobApply(jobApply);
    }

    /**
     * 批量删除岗位投递
     * 
     * @param applyIds 需要删除的岗位投递主键
     * @return 结果
     */
    @Override
    public int deleteJobApplyByApplyIds(Long[] applyIds)
    {
        return jobApplyMapper.deleteJobApplyByApplyIds(applyIds);
    }

    /**
     * 删除岗位投递信息
     * 
     * @param applyId 岗位投递主键
     * @return 结果
     */
    @Override
    public int deleteJobApplyByApplyId(Long applyId)
    {
        return jobApplyMapper.deleteJobApplyByApplyId(applyId);
    }

    @Override
    public Boolean selectJobApplyByJobIdAndUserId(Long jobId, Long userId) {
        return jobApplyMapper.selectJobApplyByJobIdAndUserId(jobId, userId) != null;
    }

    @Override
    public List<JobApplyWithJobVO> selectJobApplyWithJobList(JobApply jobApply) {
        return jobApplyMapper.selectJobApplyWithJobList(jobApply);
    }
}
